import java.rmi.StubNotFoundException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by aleks_000 on 15.12.13.
 */
public class StudentRepository extends BaseRepository<Student> {

    public StudentRepository(Connection connection) throws SQLException {
        super(connection);
    }

    @Override
    public Collection<Student> getAll() throws SQLException {
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("SELECT * FROM Student");
        Collection<Student> result = new ArrayList<Student>();
        while (resultSet.next()) {
            Student student = new Student();
            student.setId(resultSet.getInt("id"));
            student.setName(resultSet.getString("name"));
            student.setGroupNumber(resultSet.getString("groupNumber"));
            student.setMark(resultSet.getDouble("mark"));
            result.add(student);
        }
        return result;
    }

    @Override
    public Student get(int id) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Student WHERE id = ?");
        preparedStatement.setInt(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet != null && resultSet.next()) {
            Student student = new Student();
            student.setId(id);
            student.setName(resultSet.getString("name"));
            student.setGroupNumber(resultSet.getString("groupNumber"));
            student.setMark(resultSet.getDouble("mark"));
            return student;
        }
        else {
            return null;
        }
    }

    @Override
    public Student create(Student obj) throws SQLException {
        if (obj == null) {
            throw new IllegalArgumentException();
        }
        PreparedStatement statement = connection.prepareStatement("INSERT INTO Student (name, groupNumber, mark) VALUES (?, ?, ?)",
                Statement.RETURN_GENERATED_KEYS);
        statement.setString(1, obj.getName());
        statement.setString(2, obj.getGroupNumber());
        statement.setDouble(3, obj.getMark());
        statement.execute();
        ResultSet generatedKeys = statement.getGeneratedKeys();
        // todo: retrieve real value from db
        Student student = new Student();
        student.setName(obj.getName());
        student.setGroupNumber(obj.getGroupNumber());
        student.setMark(obj.getMark());
        if (generatedKeys != null && generatedKeys.next()) {
            student.setId(generatedKeys.getInt(1));
        }
        return student;
    }

    @Override
    public boolean update(Student obj) throws SQLException {
        if (obj == null) {
            throw new IllegalArgumentException();
        }
        PreparedStatement preparedStatement = connection.prepareStatement("UPDATE Student SET name = ?, groupNumber = ?, mark = ? WHERE id = ?");
        preparedStatement.setString(1, obj.getName());
        preparedStatement.setString(2, obj.getGroupNumber());
        preparedStatement.setDouble(3, obj.getMark());
        preparedStatement.setInt(4, obj.getId());
        int affectedRow = preparedStatement.executeUpdate();
        return affectedRow != 0;
    }

    @Override
    public boolean delete(Student obj) throws SQLException {
        if (obj == null) {
            throw new IllegalArgumentException();
        }
        return delete(obj.getId());
    }

    @Override
    public boolean delete(int id) throws SQLException {
        PreparedStatement statement = connection.prepareStatement("DELETE FROM Student WHERE id = ?");
        statement.setInt(1, id);
        int rowAffected = statement.executeUpdate();
        return rowAffected != 0;
    }
}
